//
//  CXOtherTableViewCell.m
//  GiftThat
//
//  Created by ChuXiang on 16/1/7.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "CXOtherTableViewCell.h"
#import "CXOtherCollectionViewCell.h"
@interface CXOtherTableViewCell () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) NSMutableArray *data;

@end

@implementation CXOtherTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.data = [NSMutableArray arrayWithCapacity:0];
        
        self.titleLabel = [UILabel new];
        [self.titleLabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:15]];
        self.titleLabel.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.titleLabel];
        
        UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc] init];
        flowLaout.scrollDirection = UICollectionViewScrollDirectionVertical;

        
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLaout];
        self.collection.delegate = self;
        self.collection.dataSource = self;
        self.collection.backgroundColor = [UIColor whiteColor];
        [self.collection registerClass:[CXOtherCollectionViewCell class] forCellWithReuseIdentifier:@"CXOtherCollectionViewCellIdentifier"];
        [self addSubview:self.collection];

    }
    return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    self.titleLabel.frame = CGRectMake(13, 0, self.width, 40);
    self.collection.frame = CGRectMake(0, self.titleLabel.bottom, self.width, self.height - 40);

}

- (void)setModel:(CXClassProModel *)model {
    if (self.model != model) {
        if (model) {
            self.titleLabel.text = model.name;
            self.data = model.channel;
            [self.collection reloadData];
        }
    }
    //    夜间模式换cell得背景颜色
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Color"] isEqualToString:@"BlackColor"]) {
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:34 / 255.0 blue:38 / 255.0 alpha:1.0];
        self.collection.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:34 / 255.0 blue:38 / 255.0 alpha:1.0];

        self.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:34 / 255.0 blue:38 / 255.0 alpha:1.0];
        
    }else{
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.backgroundColor = [UIColor whiteColor];
        self.collection.backgroundColor = [UIColor whiteColor];
        self.backgroundColor =  [UIColor whiteColor];
    }

}


#pragma mark - CollectionDataSource;

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.data.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CXOtherCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXOtherCollectionViewCellIdentifier" forIndexPath:indexPath];
    
    //    夜间模式换cell得背景颜色
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Color"] isEqualToString:@"BlackColor"]) {
        item.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:34 / 255.0 blue:38 / 255.0 alpha:1.0];
        
    }else{
        item.backgroundColor =  [UIColor whiteColor];
    }

    item.model = self.data[indexPath.item];
    return item;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CXOtherModel *model = self.data[indexPath.item];
    self.myBlock(model.NewId, model.name);
}

#pragma mark - 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}
#pragma mark - 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}
// 单元格大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size;


       size = CGSizeMake(self.width / 4 - 10, self.width / 4 - 10);
        

    

    
    return size;
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
